﻿using System;
using System.Windows.Forms;

namespace Editor
{
    partial class EditorWindow
    {
        private void UpdateEditTypeButtons()
        {
            tsb_EditSelect.Checked = (m_EditType == EditType.Select);
            tsb_EditMove.Checked = (m_EditType == EditType.Move);
            tsb_EditRotate.Checked = (m_EditType == EditType.Rotate);
            tsb_EditScale.Checked = (m_EditType == EditType.Scale);
        }

        private void UpdateUndoRedoStates(Command.ICommand command, Command.ExecutionType executionType)
        {
            UpdateUndoRedoStates();
        }

        private void UpdateUndoRedoStates()
        {
            bool bCanUndo = (m_Document != null) && (m_Document.CommandManager.CanUndo);
            bool bCanRedo = (m_Document != null) && (m_Document.CommandManager.CanRedo);

            mnu_MainMenuEditUndo.Enabled = bCanUndo;
            mnu_MainMenuEditRedo.Enabled = bCanRedo;
            tsb_Undo.Enabled = bCanUndo;
            tsb_Redo.Enabled = bCanRedo;
        }

        private void OnActionNewScene()
        {
            if (MessageBox.Show(this,
                "Do you really want to clear the scene and create a new one?\n\n" +
                "Any changes to the existing scene will be lost.",
                "Create New Scene",
                MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                CreateNewDocument();
            }
        }

        private void OnActionOpenScene()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.DefaultExt = "scn";
            ofd.DereferenceLinks = true;
            ofd.FileName = (!String.IsNullOrEmpty(Properties.Settings.Default.LastOpenDocument)) ? System.IO.Path.GetFileName(Properties.Settings.Default.LastOpenDocument) : null;
            ofd.Filter = "Scene Files (*.scn)|*.scn|All Files (*.*)|*.*";
            ofd.FilterIndex = 0;
            ofd.InitialDirectory = (!String.IsNullOrEmpty(Properties.Settings.Default.LastOpenDocument)) ? System.IO.Path.GetDirectoryName(Properties.Settings.Default.LastOpenDocument) : Application.StartupPath;
            ofd.Multiselect = false;
            ofd.RestoreDirectory = true;
            ofd.SupportMultiDottedExtensions = true;
            ofd.Title = "Open Scene";
            ofd.ValidateNames = true;

            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                Properties.Settings.Default.LastOpenDocument = ofd.FileName;
                Properties.Settings.Default.Save();

                LoadDocument(ofd.FileName);
            }
        }

        private void OnActionSaveScene()
        {
            SaveDocument(m_Document.SceneFileName);
        }

        private void OnActionSaveSceneAs()
        {
            SaveDocument(null);
        }

        private void OnActionUndo()
        {
            m_Document.CommandManager.Undo();
        }

        private void OnActionRedo()
        {
            m_Document.CommandManager.Redo();
        }
    }
}
